package ${package}.controller;

import com.njzxw.fs.commonutil.test.BaseJunit;
import org.springframework.boot.test.context.SpringBootTest;
import org.testng.annotations.Test;
import net.sf.json.JSONObject;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.annotation.Rollback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.testng.Assert.*;

@SpringBootTest(classes = ${app}Application.class)
@Rollback(value = true)
public class ${className}ControllerTest extends BaseJunit {

    private String title = "接口标题1";

    /**
     * 存放调用新增后 的数据ID值
     */
    private Integer addId = null;

    @Test(dependsOnMethods = {"queryId"})
    public void queryPage(){

        try{
            RequestTest requestTest = new RequestTest("分页查询");
            List<RequestParams> params = new ArrayList<>();
            <#list colimnList as p>
            <#if p.COLUMN_NAME != 'oper_date'>
            params.add(new RequestParams("${p.COLUMN_NAME}","1",false,"${p.COLUMN_COMMENT}"));
            </#if>
            </#list>

            params.add(new RequestParams("pageSize","1",true,"每页数量"));

            params.add(new RequestParams("pageNum","1",true,"页码"));

            params.add(new RequestParams("token",requestTest.getToken("1"),false,"token值"));

            requestTest.setJsonParams(params);

            requestTest.execute();

            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("state","状态：1成功");
            resultMap.put("data","返回的数据，当state1时才会存在数据");
            resultMap.put("msg","失败或成功提示消息");
            <#list colimnList as p>
            resultMap.put("${p.COLUMN_NAME}","${p.COLUMN_COMMENT}");
            </#list>

            requestTest.createDoc(title,"${tableComment}","接口描述",resultMap);

        }catch (Exception e){
            logger.error("测试用例出错：",e);
            assertTrue(false);
        }

    }

    @Test(dependsOnMethods = {"insert"})
    public void queryId(){

        try{
            RequestTest requestTest = new RequestTest("查询单条数据");
            List<RequestParams> params = new ArrayList<>();

            params.add(new RequestParams("id",addId,true,"数据ID")
            .addError("","未选择数据"));

            params.add(new RequestParams("token",requestTest.getToken("1"),false,"token值"));

            requestTest.setJsonParams(params);

            requestTest.execute();

            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("state","状态：1成功");
            resultMap.put("data","返回的数据，当state1时才会存在数据");
            resultMap.put("msg","失败或成功提示消息");
            <#list colimnList as p>
            resultMap.put("${p.COLUMN_NAME}","${p.COLUMN_COMMENT}");
            </#list>

            requestTest.createDoc(title,"${tableComment}","接口描述",resultMap);

        }catch (Exception e){
            logger.error("测试用例出错：",e);
            assertTrue(false);
        }
    }

    @Test(dependsOnMethods = {"queryPage"})
    public void update(){

        try{
            RequestTest requestTest = new RequestTest("修改接口");
            List<RequestParams> params = new ArrayList<>();
            params.add(new RequestParams("id",addId,true,"数据ID")
            .addError("","未选择数据"));
            <#list colimnList as p>
            <#if p.COLUMN_NAME != 'oper_date'>
            <#if p.IS_NULLABLE == 1 >
            params.add(new RequestParams("${p.COLUMN_NAME}","1",true,"${p.COLUMN_COMMENT}")
            .addError("","${p.COLUMN_COMMENT}不可为空"));
            <#else>
            params.add(new RequestParams("${p.COLUMN_NAME}","1",false,"${p.COLUMN_COMMENT}"));
            </#if>
            </#if>
            </#list>

            params.add(new RequestParams("token",requestTest.getToken("1"),false,"token值"));

            requestTest.setJsonParams(params);

            requestTest.execute();

            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("state","状态：1成功");
            resultMap.put("data","返回的数据，当state1时才会存在数据");
            resultMap.put("msg","失败或成功提示消息");

            requestTest.createDoc(title,"${tableComment}","接口描述",resultMap);

        }catch (Exception e){
            logger.error("测试用例出错：",e);
            assertTrue(false);
        }
    }

    @Test(dependsOnMethods = {"update"})
    public void delete(){

        try{
            RequestTest requestTest = new RequestTest("删除接口");
            List<RequestParams> params = new ArrayList<>();
            List<String> list = new ArrayList<String>();
            list.add(addId+"");
            params.add(new RequestParams("ids",list,true,"数据ID")
            .addError(null,"未选择数据"));

            params.add(new RequestParams("token",requestTest.getToken("1"),false,"token值"));

            requestTest.setJsonParams(params);

            requestTest.execute();

            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("state","状态：1成功");
            resultMap.put("data","返回的数据，当state1时才会存在数据");
            resultMap.put("msg","失败或成功提示消息");

            requestTest.createDoc(title,"${tableComment}","接口描述",resultMap);

        }catch (Exception e){
            logger.error("测试用例出错：",e);
            assertTrue(false);
        }

    }

    @Test
    public void insert(){

        try{
            RequestTest requestTest = new RequestTest("新增接口");
            List<RequestParams> params = new ArrayList<>();
            <#list colimnList as p>
            <#if p.COLUMN_NAME != 'oper_date'>
            <#if p.IS_NULLABLE == 1 >
            params.add(new RequestParams("${p.COLUMN_NAME}","1",true,"${p.COLUMN_COMMENT}")
            .addError("","${p.COLUMN_COMMENT}不可为空"));
            <#else>
            params.add(new RequestParams("${p.COLUMN_NAME}","1",false,"${p.COLUMN_COMMENT}"));
            </#if>
            </#if>
            </#list>

            params.add(new RequestParams("token",requestTest.getToken("1"),false,"token值"));

            requestTest.setJsonParams(params);

            MockHttpServletResponse response = requestTest.execute();

            addId = Integer.valueOf(JSONObject.fromObject(response.getContentAsString()).getString("data"));

            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("state","状态：1成功");
            resultMap.put("data","返回的数据，当state1时才会存在数据");
            resultMap.put("msg","失败或成功提示消息");

            requestTest.createDoc(title,"${tableComment}","接口描述",resultMap);

        }catch (Exception e){
            logger.error("测试用例出错：",e);
            assertTrue(false);
        }
    }

    @Test
    public void save(){

        try{
        RequestTest requestTest = new RequestTest("保存接口");
        List<RequestParams> params = new ArrayList<>();

        <#list colimnList as p>
        <#if p.COLUMN_NAME != 'oper_date'>
        <#if p.IS_NULLABLE == 1 >
                params.add(new RequestParams("${p.COLUMN_NAME}","1",true,"${p.COLUMN_COMMENT}")
                .addError("","${p.COLUMN_COMMENT}不可为空"));
        <#else>
                params.add(new RequestParams("${p.COLUMN_NAME}","1",false,"${p.COLUMN_COMMENT}"));
        </#if>
</#if>
        </#list>

        params.add(new RequestParams("token",requestTest.getToken("1"),false,"token值"));

        requestTest.setJsonParams(params);

        MockHttpServletResponse response = requestTest.execute();

        addId = Integer.valueOf(JSONObject.fromObject(response.getContentAsString()).getString("data"));

        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("state","状态：1成功");
        resultMap.put("data","返回的数据，当state1时才会存在数据");
        resultMap.put("msg","失败或成功提示消息");

        requestTest.createDoc(title,"${tableComment}","保存操作，根据ID是否存在自动跳转新增还是修改",resultMap);

        }catch (Exception e){
        logger.error("测试用例出错：",e);
        assertTrue(false);
        }
        }

}

